package tum.in.rost.pp;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class DataSplit {
	public static String NEW_LINE = "\n";

	private String newLine, lastfeature;

	private String[] feat;

	boolean dataStarted = false;

	private ArrayList<String> features = new ArrayList<String>();

	private BufferedReader bis;

	private BufferedWriter bisWriteTrain, bisWriteCTrain, bisWriteTest;

	public static final String RELATION = "@RELATION";

	public static final String TRAIN_ARFF_CONSTANT = "'train.arff'\n\n";

	public static final String CTRAIN_ARFF_CONSTANT = "'ctrain.arff'\n\n";

	public static final String TEST_ARFF_CONSTANT = "'test.arff'\n\n";

	
	
	public void splitDataForEvaluation(String dataset){
		
	}
	public void splitdata(String dataset) {
		int count = 0;
		try {
			bis = new BufferedReader(new FileReader(new File(dataset)));
			bisWriteTrain = new BufferedWriter(new FileWriter(new File(
					InstanceDataParser.TRAIN_FILE_PATH)));
			bisWriteCTrain = new BufferedWriter(new FileWriter(new File(
					InstanceDataParser.CROSS_TRAIN_FILE_PATH)));
			bisWriteTest = new BufferedWriter(new FileWriter(new File(
					InstanceDataParser.TEST_FILE_PATH)));
			while ((newLine = bis.readLine()) != null) {
				if (newLine.startsWith(RELATION)) {
					bisWriteTrain.write(RELATION + TRAIN_ARFF_CONSTANT);
					bisWriteCTrain.write("@RELATION " + CTRAIN_ARFF_CONSTANT);
					bisWriteTest.write("@RELATION " + TEST_ARFF_CONSTANT);
				}
				if (newLine.startsWith("@ATTRIBUTE")) {
					bisWriteTrain.write(newLine + NEW_LINE);
					bisWriteCTrain.write(newLine + NEW_LINE);
					bisWriteTest.write(newLine + NEW_LINE);
				}
				if (newLine.startsWith("@DATA")) {
					dataStarted = true;
					bisWriteTrain.write(newLine + NEW_LINE);
					bisWriteCTrain.write(newLine + NEW_LINE);
					bisWriteTest.write(newLine + NEW_LINE);
					continue;
				}
				if (dataStarted) {
					feat = newLine.split(",", 2);
					int splitpoint = feat[0].lastIndexOf("_");
					feat[0] = feat[0].substring(0, splitpoint - 1);

					if (!features.contains(feat[0])) {
						features.add(feat[0]);
					}
					if ((lastfeature != null) && (!feat[0].equals(lastfeature))) {
						count++;
					}
					switch (count % 3) {
					case 0:
						bisWriteTrain.write(newLine + NEW_LINE);
						break;
					case 1:
						bisWriteCTrain.write(newLine + NEW_LINE);
						break;
					case 2:
						bisWriteTest.write(newLine + NEW_LINE);
						break;
					default:
						break;
					}
					lastfeature = feat[0];
				}
			}
			bis.close();
			bisWriteTrain.close();
			bisWriteCTrain.close();
			bisWriteTest.close();

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				bis.close();
				bisWriteTrain.close();
				bisWriteCTrain.close();
				bisWriteTest.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}